package integral

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	integral "github.com/flipped-aurora/gin-vue-admin/server/model/integral"
	integralReq "github.com/flipped-aurora/gin-vue-admin/server/model/integral/request"
)

type IntegralsService struct{}

// CreateIntegrals 创建integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) CreateIntegrals(integrals *integral.Integrals) (err error) {
	err = global.GVA_DB.Create(integrals).Error
	return err
}

// DeleteIntegrals 删除integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) DeleteIntegrals(id string) (err error) {
	err = global.GVA_DB.Delete(&integral.Integrals{}, "id = ?", id).Error
	return err
}

// DeleteIntegralsByIds 批量删除integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) DeleteIntegralsByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]integral.Integrals{}, "id in ?", ids).Error
	return err
}

// UpdateIntegrals 更新integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) UpdateIntegrals(integrals integral.Integrals) (err error) {
	err = global.GVA_DB.Model(&integral.Integrals{}).Where("id = ?", integrals.Id).Updates(&integrals).Error
	return err
}

// GetIntegrals 根据id获取integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) GetIntegrals(id string) (integrals integral.Integrals, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&integrals).Error
	return
}

// GetIntegralsInfoList 分页获取integrals表记录
// Author [yourname](https://github.com/yourname)
func (integralsService *IntegralsService) GetIntegralsInfoList(info integralReq.IntegralsSearch) (list []integral.Integrals, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&integral.Integrals{})
	var integralss []integral.Integrals
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&integralss).Error
	return integralss, total, err
}
func (integralsService *IntegralsService) GetIntegralsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
